import { Camera, Component, Node, tween, v3, Vec3, _decorator } from 'cc';

const { ccclass, property } = _decorator;

@ccclass('TargetCamera')
export default class TargetCamera extends Component {
	@property(Node)
	target: Node = null!;

	private cameraPosition: Vec3 = v3();
	private targetPosition: Vec3 = v3();

	public setTarget(target: Node) {
		this.target = target;
	}

	public capture() {
		const camera = this.getComponent(Camera);
		camera.orthoHeight = 500;
		tween(camera).to(0.4, { orthoHeight: 800 }).start();
	}

	update(dt: number) {
		if (this.target) {
			this.target.getPosition(this.targetPosition);
			this.node.getPosition(this.cameraPosition);
			Vec3.lerp(this.cameraPosition, this.cameraPosition, this.targetPosition, dt * 30);
			this.cameraPosition.z = 1000;
			this.node.setPosition(this.cameraPosition);
		}
	}
}
